/**
 * <p>Title: FindLimitDir.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-8-1 上午09:30:10  张志芳  Create
 *</pre>
 */
package com.baosight.sczx.zm.cd.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.dao.Dao;

/**
 * <p>Title: FindLimitDir.java</p>
 * <p>Description:查找有权限的指定目录 </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-8-1 上午09:30:10  张志芳  Create
 *</pre>
 */
public class FindLimitDir {
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
	/**
	 * 
	 * <p>Function: 构造函数</p>
	 * <p>Description: </p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-1上午09:43:30 张志芳
	 * </p>
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public FindLimitDir()
	{
		Map _tempMap = new HashMap();
		_tempMap.put("fdOrgId", UserSession.getInSessionProperty("orgCode"));
		List<Map> Nodes = dao.query("ZMCDDirectory.queryLimitDir", _tempMap);//查询登录人可以看到目录				
		List<Map> allNodes = dao.query("ZMCDDirectory.queryAll", null);//查询所有的目录
		
		limitNodes.addAll(Nodes);
		
		//用递归的方法找到登录人可以看到所有的有权限结点。
		findParentDir(Nodes, allNodes);
		findChildDir(Nodes, allNodes);
				
	}
	
	@SuppressWarnings("rawtypes")
	List<Map> limitNodes = new ArrayList<Map>();   //最终的所有节点
	
	/**
	 * 获得最终的所有节点
	 * <p>Function: getLimitNodes</p>
	 * <p>Description: </p>
	 * <p>return value:List<Map></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-1上午09:45:05 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public List<Map> getLimitNodes() {
		return this.limitNodes;
	}
	
	/**
	 * 递归查找Nodes节点的父级目录
	 * <p>Function: findParentDir</p>
	 * <p>Description: </p>
	 * <p>return value:List<Map></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:47:21 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void findParentDir(List<Map> Nodes, List<Map> allNodes)
	{
		List<Map> list = new ArrayList<Map>();
		for (Iterator it = Nodes.iterator(); it.hasNext();) {
			Map node = (Map) it.next();
			for (Iterator it1 = allNodes.iterator(); it1.hasNext();) {
				Map node1 = (Map) it1.next();
				if(node.get("fdSuperDirId").equals(node1.get("fdKeyId")))
				{
					Map map = new HashMap();
					map.put("fdKeyId", node1.get("fdKeyId"));
					map.put("fdDirName", node1.get("fdDirName"));				
					map.put("fdSuperDirId", node1.get("fdSuperDirId"));
					map.put("fdSerial", node1.get("fdSerial"));
					map.put("fdLimit", "-1");
					list.add(map);
					break;
				}
			}
		}
		combineNode(limitNodes, list);
		if(list.size()>0)
		{			
			findParentDir(list, allNodes);
		}
	}
	
	/**
	 * 递归查找Nodes节点的子级节点
	 * <p>Function: findChildDir</p>
	 * <p>Description: </p>
	 * <p>return value:List<Map></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:47:21 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void findChildDir(List<Map> Nodes, List<Map> allNodes)
	{
		List<Map> list = new ArrayList<Map>();
		for (Iterator it = Nodes.iterator(); it.hasNext();) {
			Map node = (Map) it.next();
			for (Iterator it1 = allNodes.iterator(); it1.hasNext();) {
				Map node1 = (Map) it1.next();
				if(node.get("fdKeyId").equals(node1.get("fdSuperDirId")))
				{
					Map map = new HashMap();
					map.put("fdKeyId", node1.get("fdKeyId"));
					map.put("fdDirName", node1.get("fdDirName"));				
					map.put("fdSuperDirId", node1.get("fdSuperDirId"));
					map.put("fdSerial", node1.get("fdSerial"));
					map.put("fdLimit", node.get("fdLimit"));
					list.add(map);
				}
			}
		}
		combineNode(limitNodes, list);
		if(list.size()>0)
		{
			findChildDir(list, allNodes);
		}
	}
	
	/**
	 * 将nodes合并到Nodes中，要去除重复的节点
	 * <p>Function: combineNode</p>
	 * <p>Description: </p>
	 * <p>return value:List<Map></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:47:21 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private void combineNode(List<Map> Nodes, List<Map> nodes)
	{
		for (int i=0; i<nodes.size(); i++) {
			Map node = nodes.get(i);
			int j=0;
			for (j=0; j<Nodes.size(); j++) {
				if(node.get("fdKeyId").equals(Nodes.get(j).get("fdKeyId")))
				{	
					nodes.remove(node);
					break;
				}
			}
			if(j>=Nodes.size())
			{
				Nodes.add(node);
			}
		}
	}
	
	/**
	 * 判断List中是否已经存在目录
	 * <p>Function: contains</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-29下午05:05:59 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public boolean contains(List<Map> list, String dir) {
		for (Iterator it = list.iterator(); it.hasNext();) {
			Map node = (Map) it.next();
			if (node.get("fdKeyId").equals(dir)) {
				return true;
			}
		}
		return false;
	}
}
